<template>
	<a-row>
		<a-col :span="24">
			<a-card :bordered="false" style="margin-bottom: 10px">
				<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
					<a-row :gutter="24">
						<a-col :span="6">
							<a-form-item label="名称" name="name">
								<a-input v-model:value="searchFormState.name" placeholder="请输入名称" />
							</a-form-item>
						</a-col>
            <a-col :span="6">
            	<a-form-item label="专业" name="major">
					<a-select
						v-model:value="searchFormState.major"
						:options="majorOptions"
						style="width: 100%"
						placeholder="请选择"
						allow-clear
					/>
            	</a-form-item>
            </a-col>
						<a-col :span="4">
							<a-form-item label="年级" name="year">
								<a-input-number v-model:value="searchFormState.year" placeholder="请输入年级" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="6">
							<a-button type="primary" @click="table.refresh(true)">查询</a-button>
							<a-button style="margin: 0 8px" @click="reset">重置</a-button>
						</a-col>
					</a-row>
				</a-form>
			</a-card>
			<a-card :bordered="false">
				<s-table
					ref="table"
					:columns="columns"
					:data="loadData"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:tool-config="toolConfig"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('eduKlassAdd')">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete
								v-if="hasPerm('eduKlassBatchDelete')"
								:selectedRowKeys="selectedRowKeys"
								@batchDelete="deleteBatchEduKlass"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'type'">
							{{ $TOOL.dictTypeData('STUDENT_TYPE', record.type) }}
						</template>
						<template v-if="column.dataIndex === 'major'">
							{{ $TOOL.dictTypeData('MAJOR', record.major) }}
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a @click="formRef.onOpen(record)" v-if="hasPerm('eduKlassEdit')">编辑</a>
								<a-divider type="vertical" v-if="hasPerm(['eduKlassEdit', 'eduKlassDelete'], 'and')" />
								<a-popconfirm title="确定要删除吗？" @confirm="deleteEduKlass(record)">
									<a-button type="link" danger size="small" v-if="hasPerm('eduKlassDelete')">删除</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
	</a-row>
	<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="klass">
	import { Empty } from 'ant-design-vue'
	import Form from './form.vue'
	import eduKlassApi from '@/api/biz/eduKlassApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import tool from '@/utils/tool'
	import eduTutorialApi from "@/api/biz/eduTutorialApi";
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
  //专业名称数据
  let MajorData = ref([])
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '名称',
			dataIndex: 'name'
		},
		{
			title: '专业名称',
			dataIndex: 'major'
		},
		{
			title: '类型',
			dataIndex: 'type'
		},
		{
			title: '辅导员',
			dataIndex: 'owner'
		},
		{
			title: '人数',
			dataIndex: 'size'
		},
		{
			title: '年级',
			dataIndex: 'year'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['eduKlassEdit', 'eduKlassDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	// 学期分类字典
	const majorOptions = tool.dictList('MAJOR')
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 默认展开的节点
	let defaultExpandedKeys = ref([])
	const treeData = ref([])
	// 替换treeNode 中 title,key,children
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const cardLoading = ref(true)
  const filterOption = (input, option) => {
    return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  }
	const loadData = (parameter) => {
    seleMajorData()
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return eduKlassApi.eduKlassPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
  const seleMajorData = () => {
    const param = {}
    eduKlassApi.eduMajorList(param).then((res) => {
      MajorData.value = res
    })
  }
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 加载左侧的树
	bizOrgApi
		.orgTree()
		.then((res) => {
			cardLoading.value = false
			if (res !== null) {
				treeData.value = res
				// 默认展开2级
				treeData.value.forEach((item) => {
					// 因为0的顶级
					if (item.parentId === '0') {
						defaultExpandedKeys.value.push(item.id)
						// 取到下级ID
						if (item.children) {
							item.children.forEach((items) => {
								defaultExpandedKeys.value.push(items.id)
							})
						}
					}
				})
			}
		})
		.finally(() => {
			cardLoading.value = false
		})
	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.orgId = selectedKeys.toString()
		} else {
			delete searchFormState.orgId
		}
		table.value.refresh(true)
	}
	// 删除
	const deleteEduKlass = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		eduKlassApi.eduKlassDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchEduKlass = (params) => {
		eduKlassApi.eduKlassDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style scoped>
	.cardImp {
		margin-right: 10px;
	}
	.ant-form-item {
		margin-bottom: 0 !important;
	}
	.primaryAdd {
		margin-right: 10px;
	}
	.snowy-buttom-left {
		margin-left: 8px;
	}
</style>
